package message.types.data;

import java.io.IOException;

import message.Message;
import message.visitor.MessageVisitor;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import stalker.Stalker;
import data.datas.Datas;
import data.datas.DatasParser;

public class GetAllDatasAnswer extends Message {

private final Datas datas_;
  
  /**
   * Constructs an answer for GetLastDataRequest
   * @requires parameters not null
   * @param client      The answerer
   * @param destination The recipient
   * @param data        The answer
   */
  
  public GetAllDatasAnswer(Stalker client, Stalker destination, Datas datas) {
    super(client, destination);
    assert datas != null;
    datas_ = datas;
  }
  
  /**
   * Retrieve data from the net
   * @requires parameters not null
   * @param client      The answerer
   * @param destination The recipient
   * @param reader      The net
   * @throws IOException 
   */
  
  public GetAllDatasAnswer(Stalker client, Stalker destination, FrameReader reader) throws IOException {
    super(client, destination);
    assert reader != null;
    datas_ = DatasParser.retrieve(reader);
  }
  
  /**
   * Retrieves the answer
   * @return the answer
   */
  public Datas getAnswer() {
    return datas_;
  }
  
  @Override
  public String toString() {
    return "GetAllDatasAnswer by " + this.getClient();
  }

  @Override
  public <T> T accept(MessageVisitor<T> visitor) {
    return visitor.visit(this);
  }

  @Override
  public void send(FrameWriter writer) throws IOException {
    DatasParser.send(datas_, writer);
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof GetAllDatasAnswer)) {
      return false;
    }
    GetAllDatasAnswer that = (GetAllDatasAnswer) obj;
    return this.getClient().equals(that.getClient()) &&
           this.getDestination().equals(that.getDestination()) &&
           this.getAnswer().equals(that.getAnswer());
  }

}
